home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Text / WASTE / WASTE 1.2a2 / WESelectors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-12  |  4.6 KB  |  160 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WESelectors.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Routines for manipulating selector lookup tables
  6.  *
  7.  *  Copyright (c) 1993-1995 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. // MPW can't handle FIELD_DESC stuff (duh!)
  18.  
  19. #ifdef applec
  20.  
  21. // this is **ugly** and will break if we change the declaration of WERec
  22.  
  23. static WELookupTable _weMainSelectorTable[] = {
  24. { 'clik',0x68,0x4 },
  25. { 'drag',0x98,0x4 },
  26. { 'line',0x08,0x4 },
  27. { 'port',0x00,0x4 },
  28. { 'post',0x94,0x4 },
  29. { 'pre ',0x90,0x4 },
  30. { 'refc',0x80,0x4 },
  31. { 'runa',0x10,0x4 },
  32. { 'scrl',0x78,0x4 },
  33. { 'styl',0x0C,0x4 },
  34. { 'text',0x04,0x4 },
  35. { 'tsmd',0x84,0x4 },
  36. { 'xdrg',0xA0,0x4 },
  37. { 'c2p ',0xB4,0x4 },
  38. { 'cbyt',0xC0,0x4 },
  39. { 'ctyp',0xC4,0x4 },
  40. { 'draw',0xAC,0x4 },
  41. { 'lbrk',0xB8,0x4 },
  42. { 'wbrk',0xBC,0x4 },
  43. { 0,     0x00,0x0 }};
  44.  
  45. #if WASTE_OBJECTS
  46.  
  47. static WELookupTable _weObjectHandlerSelectorTable[] = {
  48. { 'clik',0x10,0x4 },
  49. { 'draw',0x0C,0x4 },
  50. { 'free',0x08,0x4 },
  51. { 'new ',0x04,0x4 },
  52. { 'strm',0x14,0x4 },
  53. { 0,     0x0,0x0 }};
  54.  
  55. #endif  // WASTE_OBJECTS
  56.  
  57. #else
  58.  
  59. //    proper way of defining selector tables
  60.  
  61. #define FIELD_OFFSET(FIELD, STRUCT)        (short) &((STRUCT *) 0L)->FIELD
  62. #define FIELD_SIZE(FIELD, STRUCT)        (short) sizeof(((STRUCT *) 0L)->FIELD)
  63. #define FIELD_DESC(FIELD, STRUCT)        { FIELD_OFFSET(FIELD, STRUCT), FIELD_SIZE(FIELD, STRUCT) }
  64.  
  65. static WELookupTable _weMainSelectorTable[] = {
  66. { weRefCon,             FIELD_DESC(refCon,            WERec) },
  67. { wePort,                FIELD_DESC(port,              WERec) },
  68. { weText,                FIELD_DESC(hText,             WERec) },
  69. { weCharToPixelHook,    FIELD_DESC(charToPixelHook,   WERec) },
  70. { weCharByteHook,        FIELD_DESC(charByteHook,      WERec) },
  71. { weCharTypeHook,        FIELD_DESC(charTypeHook,      WERec) },
  72. { weClickLoop,            FIELD_DESC(clickLoop,         WERec) },
  73. { weCurrentDrag,        FIELD_DESC(currentDrag,       WERec) },
  74. { weDrawTextHook,        FIELD_DESC(drawTextHook,      WERec) },
  75. { weLineBreakHook,        FIELD_DESC(lineBreakHook,     WERec) },
  76. { weLineArray,            FIELD_DESC(hLines,            WERec) },
  77. { wePixelToCharHook,    FIELD_DESC(pixelToCharHook,   WERec) },
  78. { weTSMPostUpdate,        FIELD_DESC(tsmPostUpdate,     WERec) },
  79. { weTSMPreUpdate,        FIELD_DESC(tsmPreUpdate,      WERec) },
  80. { weRunArray,            FIELD_DESC(hRuns,             WERec) },
  81. { weScrollProc,            FIELD_DESC(scrollProc,        WERec) },
  82. { weStyleTable,            FIELD_DESC(hStyles,           WERec) },
  83. { weTSMDocumentID,        FIELD_DESC(tsmReference,      WERec) },
  84. { weWordBreakHook,        FIELD_DESC(wordBreakHook,     WERec) },
  85. { weTranslateDragHook,    FIELD_DESC(translateDragHook, WERec) },
  86. { 0,                     0, 0 }};
  87.  
  88. #if WASTE_OBJECTS
  89.  
  90. static WELookupTable _weObjectHandlerSelectorTable[] = {
  91. { weClickHandler,        FIELD_DESC(clickHandler,      WEOHTableElement) },
  92. { weDrawHandler,        FIELD_DESC(drawHandler,       WEOHTableElement) },
  93. { weDisposeHandler,        FIELD_DESC(freeHandler,       WEOHTableElement) },
  94. { weNewHandler,            FIELD_DESC(newHandler,        WEOHTableElement) },
  95. { weStreamHandler,      FIELD_DESC(streamHandler,     WEOHTableElement) },
  96. { 0,                     0, 0 }};
  97.  
  98. #endif    // WASTE_OBJECTS
  99.  
  100. #endif    // !applec
  101.  
  102. pascal void _WELookupSelector(const WELookupTable *table, WESelector selector, WEFieldDescriptor *desc)
  103. {
  104.     for ( ; table->selector != selector ; table++ )
  105.         if ( * (long *) &(table->desc) == 0L )
  106.             break;
  107.     
  108.     *desc = table->desc;
  109. }
  110.  
  111. pascal OSErr _WEGetField(const WELookupTable *table, WESelector selector, long *info, void *structure)
  112. {
  113.     WEFieldDescriptor desc;
  114.     
  115.     _WELookupSelector(table, selector, &desc);
  116.     
  117.     if (desc.fLength == 0)
  118.         return weUndefinedSelectorErr;
  119.     
  120.     *info = * (long *) ((long) structure + desc.fOffset);
  121.     return noErr;
  122. }
  123.  
  124. pascal OSErr _WESetField(const WELookupTable *table, WESelector selector, long *info, void *structure)
  125. {
  126.     WEFieldDescriptor desc;
  127.     
  128.     _WELookupSelector(table, selector, &desc);
  129.     
  130.     if (desc.fLength == 0)
  131.         return weUndefinedSelectorErr;
  132.     
  133.     * (long *) ((long) structure + desc.fOffset) = *info;
  134.     return noErr;
  135. }
  136.  
  137. pascal OSErr WEGetInfo(WESelector selector, void *info, WEHandle hWE)
  138. {
  139.     return _WEGetField(_weMainSelectorTable, selector, (long *)info, (void *)*hWE);
  140. } // WEGetInfo
  141.  
  142. pascal OSErr WESetInfo(WESelector selector, const void *info, WEHandle hWE)
  143. {
  144.     OSErr err;
  145.     
  146.     err = _WESetField(_weMainSelectorTable, selector, (long *)info, (void *)*hWE);
  147.     
  148.     // the hook fields can never be NULL, so replace any NULL field with the default address
  149.     _WESetStandardHooks(hWE);
  150.     
  151.     return err;
  152. } // WESetInfo
  153.  
  154. #if WASTE_OBJECTS
  155. // for some reason _WESetField doesn't work from other files (?)
  156. pascal OSErr _WESetHandler(WESelector selector, long *info, void *structure)
  157. {
  158.     return _WESetField(_weObjectHandlerSelectorTable, selector, info, structure);
  159. }
  160. #endif